<?php

namespace common\models;

use Yii;
use common\modules\user\models\User;

/**
 * This is the model class for table "{{%goods}}".
 *
* @property integer $id
* @property string $name
* @property integer $category_id
* @property string $sn
* @property string $format
* @property integer $num
* @property string $unit
* @property string $price
* @property string $created_at
* @property string $updated_at
* @property integer $admin_id
* @property integer $depot_id
* @property string $attachment
* @property integer $position_id
*
* @property Depot $depot
* @property GoodsCategory $category
* @property DepotPosition $position
* @property GoodsManager[] $goodsManagers
*/ 
class Goods extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%goods}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['category_id', 'num', 'admin_id', 'depot_id', 'position_id'], 'integer'],
            [['price'], 'number'],
            [['name', 'sn', 'format', 'unit','created_at','updated_at', 'attachment'], 'string', 'max' => 255],
            [['depot_id'], 'exist', 'skipOnError' => true, 'targetClass' => Depot::className(), 'targetAttribute' => ['depot_id' => 'id']],
            [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => GoodsCategory::className(), 'targetAttribute' => ['category_id' => 'id']],
            [['position_id'], 'exist', 'skipOnError' => true, 'targetClass' => DepotPosition::className(), 'targetAttribute' => ['position_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'sn' => '编号',
            'name' => '物品名字',
            'category_id' => '分类', 
            'format' => '规格',
            'num' => '数量',
            'unit' => '单位',
            'price' => '单价',
            'created_at' => '采购时间',
            'updated_at' => '修改时间',
            'admin_id' => 'Admin ID',
            'depot_id' => '仓库id',
            'attachment' => '附件',
            'position_id' => '仓位', 
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getDepot()
    {
        return $this->hasOne(Depot::className(), ['id' => 'depot_id']);
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAdmin()
    {
        return $this->hasOne(User::className(), ['id' => 'admin_id']);
    }
    public function beforeSave($insert)
    {
        if($insert)
        {
            $id=static::find()->max("id");
            $this->sn="A00".intval($id+1);
            $this->admin_id=\Yii::$app->user->identity->id;
        }
            return true;
    }
    
    public function getCategory()
    {
        return $this->hasOne(GoodsCategory::className(), ['id' => 'category_id']);
    }
    
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPosition()
    {
        return $this->hasOne(DepotPosition::className(), ['id' => 'position_id']);
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGoodsManagers()
    {
        return $this->hasMany(GoodsManager::className(), ['goods_id' => 'id']);
    } 
}
